package cn.tedu.csmall.product.repo.impl;

import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.repo.IBrandRedisRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Repository
public class BrandRedisRepositoryImpl implements IBrandRedisRepository {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    public BrandRedisRepositoryImpl() {
        log.debug("创建缓存处理对象：BrandRedisRepositoryImpl");
    }

    @Override
    public void save(BrandStandardVO brand) {
        log.debug("开始处理【向Redis中写入品牌详情】的数据访问，参数：{}", brand);
        String key = BRAND_ITEM_KEY_PREFIX + brand.getId();
        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        ops.set(key, brand);
    }

    @Override
    public void save(List<BrandListItemVO> brandList) {
        log.debug("开始处理【向Redis中写入品牌列表】的数据访问，参数：{}", brandList);
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        for (BrandListItemVO brand : brandList) {
            ops.rightPush(BRAND_LIST_KEY, brand);
        }
    }

    @Override
    public Boolean delete(Long id) {
        log.debug("开始处理【删除Redis中的品牌详情】的数据访问，参数：{}", id);
        String key = BRAND_ITEM_KEY_PREFIX + id;
        return redisTemplate.delete(key);
    }

    @Override
    public Boolean deleteList() {
        log.debug("开始处理【删除Redis中的品牌列表】的数据访问，无参数");
        Boolean result = redisTemplate.delete(BRAND_LIST_KEY);
        return result;
    }

    @Override
    public BrandStandardVO get(Long id) {
        log.debug("开始处理【从Redis中读取品牌详情】的数据访问，参数：{}", id);
        BrandStandardVO result = null;
        String key = BRAND_ITEM_KEY_PREFIX + id;
        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        Serializable serializable = ops.get(key);
        if (serializable != null) {
            // if (serializable instanceof BrandStandardVO) {
            result = (BrandStandardVO) serializable;
            // }
        }
        return result;
    }

    @Override
    public List<BrandListItemVO> list() {
        long start = 0;
        long end = -1;
        return list(start, end);
    }

    @Override
    public List<BrandListItemVO> list(long start, long end) {
        log.debug("开始处理【读取Redis中的品牌列表】的数据访问，起始下标：{}，结束下标：{}", start, end);
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        List<Serializable> list = ops.range(BRAND_LIST_KEY, start, end);
        List<BrandListItemVO> brandList = new ArrayList<>();
        for (Serializable item : list) {
            // if (item instanceof BrandListItemVO) {
            brandList.add((BrandListItemVO) item);
            // }
        }
        return brandList;
    }

}